package com.khotyn.heresy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.khotyn.heresy.bean.Album;
import com.khotyn.heresy.bean.HeresyErrorMessage;
import com.khotyn.heresy.exception.IllegalUrlParamException;
import com.khotyn.heresy.service.MovePictureService;

/**
 * 移动图片控制类
 * 
 * @author 黄挺
 * 
 */
@Controller
@RequestMapping("/movePicture.html")
public class MovePictureController {
	@Autowired
	MovePictureService movePictureService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView onLoad(HttpSession session, @RequestParam(value = "pictureID", required = false) String pictureID,
			@RequestParam(value = "ownerID", required = false) String ownerIDStr) {
		Integer albumID = (Integer) session.getAttribute("albumID");
		Integer userID = (Integer) session.getAttribute("userID");

		try {
			validate(pictureID, ownerIDStr, albumID, userID);
		} catch (IllegalUrlParamException e) {
			return e.getErrorModel();
		}

		List<Album> albums = movePictureService.getAlbumDAO().selectAlbumByUserID(userID);
		return new ModelAndView("movePicture", "albums", albums);
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView OnSubmit(HttpSession session, HttpServletRequest request,
			@RequestParam(value = "pictureID", required = false) String pictureID,
			@RequestParam(value = "ownerID", required = false) String ownerIDStr,
			@RequestParam(value = "desAlbumID", required = false) String desAlbumIDStr) {
		Integer albumID = (Integer) session.getAttribute("albumID");
		Integer userID = (Integer) session.getAttribute("userID");

		try {
			validate(pictureID, ownerIDStr, desAlbumIDStr, albumID, userID);
		} catch (IllegalUrlParamException e) {
			return e.getErrorModel();
		}

		Integer desAlbumID = Integer.parseInt(desAlbumIDStr);

		movePictureService.doService(userID, pictureID, albumID, desAlbumID);
		return new ModelAndView("redirect:/picture.html?pictureID=" + pictureID);
	}

	private void validate(String pictureID, String ownerIDStr, Integer albumID, Integer userID) throws IllegalUrlParamException {
		HeresyErrorMessage message = null;

		if (userID == null) {
			message = new HeresyErrorMessage("您尚未登录，请先登录", "未登录", "login.html");

		} else if (albumID == null) {
			message = new HeresyErrorMessage("未知的相册", "未登录", "user.html?userID=" + userID);
		} else if (!NumberUtils.isDigits(ownerIDStr) || !NumberUtils.isDigits(pictureID)) {
			message = new HeresyErrorMessage("非法的参数", "操作失败", "/Dragonfly/");
		}

		if (message != null) {
			throw new IllegalUrlParamException(message);
		}
	}

	private void validate(String pictureID, String ownerIDStr, String desAlbumIDStr, Integer albumID, Integer userID) throws IllegalUrlParamException {
		HeresyErrorMessage message = null;

		if (userID == null) {
			message = new HeresyErrorMessage("您尚未登录，请先登录", "未登录", "login.html");
		} else if (albumID == null) {
			message = new HeresyErrorMessage("未知的相册", "操作失败", "user.html?userID=" + userID);
		} else if (!NumberUtils.isDigits(desAlbumIDStr) || NumberUtils.isDigits(ownerIDStr) || NumberUtils.isDigits(pictureID)) {
			message = new HeresyErrorMessage("非法的参数", "操作失败", "/Dragonfly/");
		} else if (Integer.parseInt(ownerIDStr) != userID) {
			message = new HeresyErrorMessage("你不是这张图片的所有者", "操作失败", "/Dragonfly/");
		} else if (albumID == Integer.parseInt(desAlbumIDStr)) {
			message = new HeresyErrorMessage("不能将相册移动到同一相册", "操作失败", "picture.html?picture=" + pictureID);
		}

		if (message != null) {
			throw new IllegalUrlParamException(message);
		}

	}
}
